<%@ page import="pojo.Paper" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="pojo.Question" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
  Created by IntelliJ IDEA.
  User: 21131
  Date: 2023/11/21
  Time: 13:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>教师发布</title>
    <script src="${ pageContext.request.contextPath }/js/jquery-3.6.0.js" type="text/javascript"></script>
    <style>
        table {
            width: 1000px;
            text-align: center;
            margin-left: 20%;
            margin-top: 20px;
        }

        /*每个单元格*/
        th, td {
            border: 1px solid #999;
            text-align: center;
            padding: 20px 0;
            height: 40px;
        }

        /*偶数的tr的颜色*/
        table tr:nth-child(odd) {
            background-color: rgba(11, 114, 193, 0.45);
        }

        /*鼠标滑过的颜色*/
        table tr:hover {
            background-color: rgba(11, 114, 193, 0.25);
        }

        /*表头内容居中*/
        #mytable th {
            text-align: center;
            width: 200px;
        }

        .content_input .my-search {
            margin-left: 10px;
            margin-top: 20px;
            float: left;
            width: 30%;
            height: 38px;
            border-radius: 5px;
            color: dimgray;
            border: 1px solid #3388FF;
        }

        .content_input .search_btn { /*按钮样式*/
            width: 80px;
            height: 40px;
            margin-top: 20px;
            margin-left: 30px;
            background-color: steelblue;
        }

        .content_input .search_btn:hover { /*按钮覆盖样式*/
            background-color: #2e6da4;
            color: ivory;
        }

        /*发布按钮*/
        .fabu {
            background-color: #66afe9;
            border: 2px solid black;
            height: 38px;
            width: 90px;
        }

        /*发布按钮悬浮颜色*/
        .fabu:hover {
            background-color: rgba(11, 114, 193, 0.25);
        }

    </style>
</head>
<body>
<jsp:include page="teacher_menu.jsp"></jsp:include>
<div style="background-color:  #66afe9;display: inline-block;margin-top: 10px;float: left;margin-left: 20%;height: 80px;width: 200px;text-align: center; border-radius:0px;">
    <h1 style="color:white;text-shadow: 2px 2px 2px dodgerblue;line-height: 40px;border-radius:2px;font-size: 25px">
        我的考试</h1>
</div>
<div style="margin-left: 35%;margin-top:10px;width: 60%;height:80px;background-color: #66afe9; ">
    <form style="padding-left: 10%" action="${pageContext.request.contextPath }/search_publish" method="POST">
        <div class="content_input">
            <input class="my-search" type="text" placeholder="请输入考试名称" name="p_name">
            <input type="submit" value="查询" class="btn btn-primary search_btn">
            <form style="padding-left: 10%" action="${pageContext.request.contextPath }/loadPublishList" method="POST">
                <input type="submit" value="所有" class="btn btn-primary search_btn">
            </form>
        </div>
    </form>
</div>
<table id="mytable">
    <%--    表头--%>
    <tr style=" background-color: rgba(11,114,193,0.65);color: white;">
        <th>考试名称</th>
        <th>开始时间</th>
        <th>结束时间</th>
        <th>发布考试</th>
    </tr>
    <%--  表格内元素，所有试卷--%>
    <%
        List<Paper> cs = (List<Paper>) session.getAttribute("teacher_exam");
        if (cs.isEmpty()) {//数据为空
    %>
    <tr>
        <td>数</td>
        <td>据</td>
        <td>为</td>
        <td>空</td>
    </tr>
    <%
        }
        for (Paper c : cs) {
            String stime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getStarttime());
            String etime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getEndtime());
    %>
    <tr>
        <td><%=c.getP_name()%>
        </td>
        <td><%=stime%>
        </td>
        <td><%=etime%>
        </td>
        <% if(c.getIfpublish()==0){
        %>
        <td>
            <input type="button"  class="fabu" value="发布">
        </td><%}else {%>
        <td>
            <input type="button" class="fabu" value="已发布" disabled>
        </td>
        <%
            }}
        %>
    </tr>
</table>

</body>
</html>
<script src="js/jquery-1.12.3.min.js"></script>
<script>
    // 获取点击发布对应的试卷名
    $(function () {
        $(".fabu").click(function () {
            var name1 = $(this).parents("tr").children("td:nth-child(1)").text();//获取点击发布对应的试卷名
            $.ajax({//传数据给controller判断是否已发布
                type: "post",
                url: "${pageContext.request.contextPath }/publish",
                data: {
                    "name": name1
                },
                async: true,
                success: function (data) {
                    console.log(data);//在控制台输出信息。
                    if (data == true) {//已发布
                        alert("该试卷已发布")
                        window.location.replace("${pageContext.request.contextPath }/loadPublishList");//及时刷新页面
                    } else {//还未发布
                        alert("发布成功")
                        window.location.replace("${pageContext.request.contextPath }/loadPublishList");
                    }
                }
            });

        })
    })
</script>